package org.example.trigger.http;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.api.IUserPaticipateApiService;
import org.example.api.model.dto.LotteryRecordDTO;
import org.example.api.model.response.Response;
import org.example.domain.user_participate.model.entity.LotteryRecordEntity;
import org.example.domain.user_participate.service.IUserParticipateService;
import org.example.types.common.ResponseCode;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxc18
 * @date 2025/3/24 10:54
 * @description UserParticipateController
 */
@RestController
@Slf4j
@RequestMapping("/api/${app.config.api-version}/user-participate/")
@Tag(name = "用户参与活动")
public class UserParticipateController implements IUserPaticipateApiService {

    @Resource
    private IUserParticipateService userParticipateService;
    @Override
    @RequestMapping(value = "quota", method = RequestMethod.GET)
    @Operation(summary = "查询用户剩余额度", description = "输入用户id和活动id，返回用户剩余额度")
    public Response<Integer> queryUserParticipateQuota(@RequestParam Long userId,
                                                       @RequestParam Long activityId) {

        try {
            Integer res = userParticipateService.queryUserParticipateQuota(userId, activityId);
            log.info("查询用户{}，在活动{}，的剩余额度:{}", userId,activityId,res);
            return Response.<Integer>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(res)
                    .build();

        } catch (Exception e) {
            log.error("查询用户{}，在活动{}，的剩余额度失败", userId,activityId);
            return Response.<Integer>builder()
                    .code(ResponseCode.USER_PARTICIPATE_QUERY_REST_QUOTA_FAIL.getCode())
                    .info(ResponseCode.USER_PARTICIPATE_QUERY_REST_QUOTA_FAIL.getInfo())
                    .data(null)
                    .build();
        }

    }

    @Override
    @RequestMapping(value = "used-quota", method = RequestMethod.GET)
    @Operation(summary = "查询用户已用额度", description = "输入用户id和活动id，返回用户已用额度")
    public Response<Integer> queryUserUsedQuota(@RequestParam Long userId,
                                                @RequestParam Long activityId) {
        try {
            Integer res = userParticipateService.queryUserUsedQuota(userId, activityId);
            log.info("查询用户{}，在活动{}，的已用额度:{}", userId,activityId,res);
            return Response.<Integer>builder()
                   .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(res)
                    .build();

        }
        catch (Exception e) {
            log.error("查询用户{}，在活动{}，的已用额度失败", userId,activityId);
            return Response.<Integer>builder()
                    .code(ResponseCode.USER_PARTICIPATE_QUERY_USED_QUOTA_FAIL.getCode())
                    .info(ResponseCode.USER_PARTICIPATE_QUERY_USED_QUOTA_FAIL.getInfo())
                    .data(null)
                    .build();
        }
    }

    @Override
    @RequestMapping(value = "add-quota", method = RequestMethod.POST)
    @Operation(summary = "增加用户剩余额度", description = "输入用户id和活动id，增加的额度值，增加用户剩余额度")
    public Response<Boolean> addUserParticipateQuota(@RequestParam Long userId,
                                                     @RequestParam Long activityId,
                                                     @RequestParam int quotaNum) {
        try {
            Boolean res = userParticipateService.addUserParticipateQuota(userId, activityId, quotaNum);
            log.info("用户{}，在活动{}，增加{}额度成功", userId,activityId,quotaNum);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(res)
                    .build();
        }
        catch (Exception e) {
            log.error("用户{}，在活动{}，增加{}额度失败", userId,activityId,quotaNum);
            return Response.<Boolean>builder()
                   .code(ResponseCode.USER_PARTICIPATE_ADD_REST_QUOTA_FAIL.getCode())
                   .info(ResponseCode.USER_PARTICIPATE_ADD_REST_QUOTA_FAIL.getInfo())
                  .data(null)
                  .build();
        }

    }

    @Override
    @RequestMapping(value = "lottery-record", method = RequestMethod.GET)
    @Operation(summary = "查询用户抽奖记录", description = "输入用户id，返回用户抽奖记录")
    public Response<List<LotteryRecordDTO>> queryUserLotteryRecord(@RequestParam Long id) {
        try {
            List<LotteryRecordEntity> res = userParticipateService.queryUserLotteryRecord(id);
            List<LotteryRecordDTO> lotteryRecordDTOS = new ArrayList<>();
            for (LotteryRecordEntity recordEntity : res) {
                LotteryRecordDTO recordDTO = new LotteryRecordDTO();
                recordDTO.setRecordId(recordEntity.getRecordId());
                recordDTO.setUserId(recordEntity.getUserId());
                recordDTO.setActivityId(recordEntity.getActivityId());
                recordDTO.setStatus(recordEntity.getStatus());
                recordDTO.setCreateTime(recordEntity.getCreateTime());
                recordDTO.setUpdateTime(recordEntity.getUpdateTime());
                lotteryRecordDTOS.add(recordDTO);
            }
            log.info("查询用户{}的抽奖记录成功", id);
            return Response.<List<LotteryRecordDTO>>builder()
                  .code(ResponseCode.SUCCESS.getCode())
                  .info(ResponseCode.SUCCESS.getInfo())
                  .data(lotteryRecordDTOS)
                  .build();
        }
        catch (Exception e) {
            log.error("查询用户{}的抽奖记录失败", id);
            return Response.<List<LotteryRecordDTO>>builder()
                 .code(ResponseCode.USER_PARTICIPATE_QUERY_LOTTERY_RECORD_FAIL.getCode())
                 .info(ResponseCode.USER_PARTICIPATE_QUERY_LOTTERY_RECORD_FAIL.getInfo())
                 .data(null)
                .build();
        }
    }
}
